var util = require('../../utils/util.js');
var api = require('../../config/api.js');
var app = getApp()
Page({
    data: {
        // text:"这是一个页面"
        topicList: [],
        page: 1,
        size: 3,
        count: 0,
        scrollTop: 0,
        totalPage: 0,
        isNoMore:false
    },
    onLoad: function (options) {
      // console.log('哈哈哈')
        // 页面初始化 options为页面跳转所带来的参数
        // this.getTopic();

    },
    onReady: function () {
        // 页面渲染完成
    },
    onShow: function () {
        // 页面显示
      console.log('哈哈哈')
      this.getTopic();
    },
    onHide: function () {
        // 页面隐藏
    },
    onUnload: function () {
        // 页面关闭
    },
    // 下拉刷新
    onPullDownRefresh(){
      console.log('下拉刷新')
      this.setData({
        page:1,
        isMore:false,
        topicList:[]
      })
      this.getTopic()
    },
    // 上拉加载
    onReachBottom(){
      console.log('上拉加载',this.data.page,this.data.totalPage)
      if(this.data.page >= this.data.totalPage){
        this.setData({
          isNoMore:true
        })
        console.log("isNoMore",this.data.isNoMore)
      }else{
        var that = this;
        // if (this.data.page + 1 > that.data.count / that.data.size) {
        //   return true;
        // }
        that.setData({
          "page": parseInt(that.data.page) + 1
        });
        this.getTopic();
      }
      
    },
  
    getTopic: function(){ 
      let that = this;
      // 页面渲染完成
      wx.showToast({
          title: '加载中...',
          icon: 'loading',
          duration: 2000
      });

      util.request(api.TopicList, { page: that.data.page, size: that.data.size }).then(function (res) {
          if (res.errno === 0) {
            var topicList = that.data.topicList.concat(res.data.data)
            that.setData({
              topicList: topicList,
              count: res.data.count,
              totalPage: res.data.totalPages
            });
          }
          wx.hideToast();
        });
        
    }
})